package HIR.graph;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

import log.CpuTrace;
import log.TaskTrace;

import storm.Tasks.Task;
import storm.Tasks.Task.Activation;
import storm.Tasks.Task.States;



public class taskGantt extends Gantt   implements Runnable {
    public void run() {

    	this.historique =  task.getKernel().getLogs().getHistoTaskById(this.cdebut,this.cend,this.task);
    }
	private Task task;
	
	private ArrayList historique=null;
	
	
	private boolean drawState=true;

	private States state=null;

	private int pred=-1;
	
	public String getName(){
		return this.task.name;
	}

	public taskGantt(Task a) {
		
		super();
		this.task = a;
		
		
		// TODO Auto-generated constructor stub
	}
	
	 protected void getHisto() {
			
	 		if ((this.historique == null )  ||
	 				(this.debut < this.cdebut) ||
	 	 			(this.end > this.cend))
	 				{
	 			int intervalle =  (this.cache * (this.end - this.debut));
	 			this.cdebut = this.debut - intervalle;
	 			this.cdebut = 	(this.cdebut <  0) ? (0) : (this.cdebut);
	 			this.cend = this.end + intervalle;
	 			this.setWaitLoad();
	 			
	 			

	 			
	 		}
	 		
	}
	 protected void drawArrow(boolean sens , int date, Color color,Graphics2D graphic,boolean dash){
		 
		int x = this.dateToX(date);
		int y = (int) ((this.window.H / ratioHaut ) ) ;
		y = y +  (int) ((this.window.H  - y )-(this.window.H / ratioBas )) ;
		int y2 = 20;
		
		
			Color crt = graphic.getColor();
			float dash1[] = {10.0f};
			BasicStroke strokeCrt = (BasicStroke) ((Graphics2D) graphic).getStroke();
			if (dash){
		
			
			BasicStroke dashed = new BasicStroke(1.0f, 
	                BasicStroke.CAP_BUTT, 
	                BasicStroke.JOIN_MITER, 
	                10.0f, dash1, 0.0f);
			
			
			((Graphics2D) graphic).setStroke(dashed);
			}
			graphic.setColor(color);
			graphic.drawLine(x,y,x,y2);
			
			
			
			Polygon triangle = new Polygon();
			if(sens){
				triangle.addPoint(x-5,y2);
				triangle.addPoint(x,y2-5);
				triangle.addPoint(x+5,y2);
			}
			else{
				y = y -5;
				graphic.drawLine(x,y-5,x,y+5);
				
				triangle.addPoint(x-5,y);
				triangle.addPoint(x,y+6);
				triangle.addPoint(x+5,y);
			}
			graphic.fillPolygon(triangle);
			
			
			
			
			
			((Graphics2D) graphic).setStroke(strokeCrt);
			graphic.setColor(crt);
	 }
	 
	 protected void drawGantt(Graphics2D graphic) {
		 this.getHisto();
			Iterator i = historique.iterator();
			int y = (int) ((this.window.H / ratioHaut ) );
			int yy =  (int) ((this.window.H  - y )-(this.window.H / ratioBas )) ;
			int datepred = - 1;
			while (i.hasNext()){
				TaskTrace T = (TaskTrace) i.next();
				if (state == null ) state = T.state;
				if (pred == -1 ) pred = T.date;
				
				//System.out.println(T.state + "  " + T.CPU);
				if( (T.date>= this.cdebut ) && (T.date <= this.cend)&&(T.state == storm.Tasks.Task.States.RUNNING) && (datepred != T.date)) {
			//	if( (T.date>= this.cdebut ) && (T.date <= this.cend)&&(T.state == TaskTrace.Type.RUNNING) && (datepred != T.date)) {
					datepred = T.date;
					int x =  this.dateToX(T.date);
					
					
					float transparency=(float) Double.parseDouble(  this.Props.getProperty("gantt.taskGantt.transparence"));;
					graphic.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER,transparency));
					graphic.setColor(T.task.getColor());
					graphic.fillRect(x, y ,this.zoom, yy);
					graphic.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER,1.0f));
					
					//draw ref vs processor
					 graphic.setColor (T.CPU.getColor())  ;
					 graphic.fillRect(x, y+yy+(2*this.separationAbscisseinfoplus ),this.zoom , 5);
				}
				//draw state
				if (T.state != this.state) {
					    graphic.setColor ((Color) Gantt.colorNames.get(state.ordinal()) ) ;
					    int x3 =  this.dateToX(pred);
					    int x2 =  this.dateToX(T.date);
						graphic.fillRect(x3, y+yy+separationAbscisseinfoplus , x2-x3, 5);
						   
						state = T.state ;
						pred = T.date;
					
				}
							
			}
			// Draw Activation and deadline arrow Activation Fifo
		 LinkedList ListeActivation = this.task.getActivation(); 
		 Iterator La = ListeActivation.iterator();
		 
		while (La.hasNext()) {
			Task.Activation A = (Activation) La.next();
			if ( (A.getDate() >= this.cdebut ) &&  (A.getDate() < this.cend) ) {
				int deadline = A.getDeadline();
				// draw activation
				if(A.isLoseActivation()) {
					this.drawArrow(true, A.getDate(), Color.orange, graphic,false);
					
				}
				else {
					this.drawArrow(true, A.getDate(), Color.red, graphic,false);
					this.drawArrow(false, deadline, Color.red, graphic,false);
				}
				//draw deadline 
			
		
				
				//draw deb and deb activity
				//draw abort
				if (A.isAbort()) {
					if (A.getEnd_exec()>= 0)
					this.drawAbort(A.getEnd_exec(),graphic,Color.red,"X: "+A.getDate());
				}
				else {
					//dessine fin de travail
					if (A.getEnd_exec()>= 0) 
						this.drawMark(A.getEnd_exec(),graphic,Color.DARK_GRAY,+2,true);
				}
				if (A.isMissed() && (!A.isAbort())){
					if (A.getEnd_exec()>= 0)
						this.drawAbort(A.getDeadline(),graphic,Color.orange,"M: "+A.getDate());
				}
				if (A.getDeb_exec()>= 0 ) {
					this.drawMark(A.getDeb_exec(),graphic,Color.white,+1,false);
				}
				
			}
		}

			
			// Draw Block and message 
			
			
	 }

	private void drawMark(int endExec, Graphics2D graphic, Color color,int dec, boolean sens ) {
		// TODO Auto-generated method stub
		int x = this.dateToX(endExec);
		int h = 5 ;
		int y = (int) ((this.window.H / ratioHaut ) ) + (dec * h) ;
	
		graphic.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER,(float) 0.8));
		graphic.setColor(color);
		graphic.fillRect(x, y, this.zoom, h);
		graphic.setColor(Color.black);
		
		
		graphic.drawRect(x, y, this.zoom, h);
		y = (int) ((this.window.H / ratioHaut ) );
	
		int yy = y +  (int) ((this.window.H  - y )-(this.window.H / ratioBas )) ;
		if (sens)
		graphic.drawLine(x+zoom, y, x+zoom, yy);
		else
			graphic.drawLine(x, y, x, yy);	
		
		graphic.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER,1.0f));
		
	}

	private void drawAbort(int date, Graphics2D graphic,Color color,String mess) {
		int x = this.dateToX(date);

		int y = 2;
		graphic.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER,1.0f));
		graphic.setColor(Color.black);
		graphic.drawString(mess+"", x+2, y+12);
		graphic.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER,(float) 0.6));
		graphic.setColor(color);
		int s = graphic.getFontMetrics(this.f).stringWidth(mess+"")+4;
		graphic.fillRoundRect(x, y, s, 16,5,5);
		
		graphic.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER,1.0f));
		
		
		
		
		
	}
	
	
}
